package medium.差分数组;

/**
 * 创建时间：2025-05-21 11:50
 */
public class 拼车_1094 {
    public boolean carPooling(int[][] trips, int capacity) {
        int[] d = new int[1001]; // 数据范围最大为1000
        // 构建差分数组
        for (int[] trip : trips) {
            int num = trip[0], from = trip[1], to = trip[2];
            d[from] += num;
            d[to] -= num;
        }
        int sum = 0;
        for (int v : d) {
            sum += v;
            if (sum > capacity) {
                return false;
            }
        }
        return true;
    }
}
